﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Text;
using System.Text.Json.Nodes;

namespace AspDotNet.Core6.WebApi.Controllers
{
    public class HttpClientController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        private string GetUrl(string path)
        {
            var s = this.Url.ToString();

            var url = new StringBuilder()
             .Append(HttpContext.Request.Scheme)
             .Append("://")
             .Append(HttpContext.Request.Host)
             .Append(path)
             .ToString();
            return url.ToString();

        }

        [Produces("application/json")]
        public async Task<IActionResult> CallGetByFromQuery()
        {
            using (HttpClient httpClient = new HttpClient() { Timeout = TimeSpan.FromMinutes(15) })
            {
                var url = GetUrl("/api/HttpVerb/GetByFromQuery?name=name&age=age");
                httpClient.DefaultRequestHeaders.Add("appkey", "1234");
                httpClient.DefaultRequestHeaders.Add("appsecret", "456436");

                var result = await httpClient.GetAsync(url);

                string json = await result.Content.ReadAsStringAsync();

                return Ok(json);

            }
        }

        [Produces("application/json")]
        public async Task<IActionResult> CallPostByFormQuery()
        {
            using (HttpClient httpClient = new HttpClient() { Timeout = TimeSpan.FromMinutes(15) })
            {
                var url = GetUrl("/api/HttpVerb/PostByFormQuery?name=name&age=age");
                httpClient.DefaultRequestHeaders.Add("appkey", "1234");
                httpClient.DefaultRequestHeaders.Add("appsecret", "456436");

                var result = await httpClient.PostAsync(url,new StringContent("{\"no\":\"123456\"}",Encoding.UTF8,"application/json"));

                string json = await result.Content.ReadAsStringAsync();

                return Ok(json);

            }
        }



        [Produces("application/json")]
        public async Task<IActionResult> CallPutByFormQuery()
        {
            using (HttpClient httpClient = new HttpClient() { Timeout = TimeSpan.FromMinutes(15) })
            {
                var url = GetUrl("/api/HttpVerb/PutByFormQuery?id=15");
                httpClient.DefaultRequestHeaders.Add("appkey", "1234");
                httpClient.DefaultRequestHeaders.Add("appsecret", "456436");
                MultipartFormDataContent multipart=new MultipartFormDataContent ();
                multipart.Add(new StringContent("{\"no\":\"123456\"}", Encoding.UTF8, "application/json"));
                multipart.Add(new StringContent ("Jion"),"name");
                multipart.Add(new StringContent ("18"),"age");

                var result = await httpClient.PutAsync(url,multipart);

                string json = await result.Content.ReadAsStringAsync();

                return Ok(json);

            }
        }



        [Produces("application/json")]
        public async Task<IActionResult> CallDeleteByFormQuery()
        {
            using (HttpClient httpClient = new HttpClient() { Timeout = TimeSpan.FromMinutes(15) })
            {
                var url = GetUrl("/api/HttpVerb/DeleteByFormQuery?id=15");
                httpClient.DefaultRequestHeaders.Add("appkey", "1234");
                httpClient.DefaultRequestHeaders.Add("appsecret", "456436");
                MultipartFormDataContent multipart=new MultipartFormDataContent ();
                multipart.Add(new StringContent("{\"no\":\"123456\"}", Encoding.UTF8, "application/json"));
                multipart.Add(new StringContent ("Jion"),"name");
                multipart.Add(new StringContent ("18"),"age");

                var result = await httpClient.PutAsync(url,multipart);

                string json = await result.Content.ReadAsStringAsync();

                return Ok(json);

            }
        }


        [Produces("application/json")]
        public async Task<IActionResult> CallSendAsync()
        {
            using (HttpClient httpClient = new HttpClient() { Timeout = TimeSpan.FromMinutes(15) })
            {
                httpClient.DefaultRequestHeaders.Add("appkey", "1234");
                httpClient.DefaultRequestHeaders.Add("appsecret", "456436");
                MultipartFormDataContent multipart = new MultipartFormDataContent();
                multipart.Add(new StringContent("{\"no\":\"123456\"}", Encoding.UTF8, "application/json"));
                multipart.Add(new StringContent("Jion"), "name");
                multipart.Add(new StringContent("18"), "age");
                var request = new HttpRequestMessage
                {
                    RequestUri = new Uri(GetUrl("/api/HttpVerb/DeleteByFormQuery?id=15")),
                    Method = new HttpMethod("Delete"),
                    Content = multipart
                };

                var response = await httpClient.SendAsync(request);
                string json= await response.Content.ReadAsStringAsync();
                return Ok(json);
            }
        }
    }
}
